package hengtiansoft.com.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by lu on 16/1/19.
 */
public class CatchPacture extends Activity {

    public static String SDPATH = Environment.getExternalStorageDirectory()
            + "/Photo_LJ/";

    SurfaceView sView;
    SurfaceHolder surfaceHolder;
    int screenWidth, screenHeight;
    android.hardware.Camera camera;
    boolean isPreview;

    String imgPath = "/sdcard/test/img.jpg";

    ImageView iViewPic;

    Canvas canvas;
    private Paint paint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.catchpicture);
        //图片提取Bitmap的Alpha通道做法
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
//提取Alpha通道
//        Bitmap bitmapAlpha = bitmap.extractAlpha();
//        paint.setColor(Color.RED);
//        paint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));
////绘制模糊背景
//        canvas.drawBitmap(bitmapAlpha, 300, 500, paint);
////绘制原图
//        canvas.drawBitmap(bitmap, 300, 500, paint);

        initView();
    }

    private void initView() {
        iViewPic = (ImageView) findViewById(R.id.iview);
        //必须确保文件夹路径存在，否则拍照后无法完成回调  PS如果这样做,需要从系统中去找到文件放入
        File vFile = new File(imgPath);
        if (!vFile.exists()) {
            File vDirPath = vFile.getParentFile(); //new File(vFile.getParent());
            vDirPath.mkdirs();
        }
//        Uri uri = Uri.fromFile(vFile);
//        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//如果指定了保存目录返回的data就应该是空
//        startActivityForResult(intent, 1);
        Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(openCameraIntent, 1);


//        Button button = (Button) findViewById(R.id.button);
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(); //调用照相机
//                intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
//                startActivity(intent);
//            }
//        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && data != null) {
//            String fileName = String.valueOf(System.currentTimeMillis());
            Bitmap bm = (Bitmap) data.getExtras().get("data");
//            FileUtils.saveBitmap(bm, fileName);
            iViewPic.setImageBitmap(bm);


        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private static class FileUtils {
        public static void saveBitmap(Bitmap bm, String picName) {
            try {
                if (!isFileExist("")) {
//                    File tempf = createSDDir("");
                }
                File f = new File(SDPATH, picName + ".JPEG");
                if (f.exists()) {
                    f.delete();
                }
                FileOutputStream out = new FileOutputStream(f);
                bm.compress(Bitmap.CompressFormat.JPEG, 10, out);
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static boolean isFileExist(String fileName) {
            File file = new File(SDPATH + fileName);
            file.isFile();
            return file.exists();
        }
    }


}
//Matrix matrix = new Matrix();
//matrix.setRotate(90);
//        FileInputStream fis;
//        Bitmap bitmap = null;
//        try {
//        fis = new FileInputStream(imgPath);
//        bitmap = BitmapFactory.decodeStream(fis);
//        } catch (FileNotFoundException e) {
//        e.printStackTrace();
//        }
//        ByteArrayOutputStream baos = null ;
//        try{
//        baos = new ByteArrayOutputStream();
//        bitmap.compress(Bitmap.CompressFormat.JPEG, 10, baos);
//        }finally{
//        try {
//        if(baos != null)
//        baos.close() ;
//        } catch (IOException e) {
//        e.printStackTrace();
//        }
//        }
//        Bitmap bitmaps = Bitmap.createBitmap(
//        bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
//        iViewPic.setImageBitmap(bitmaps);
//            iViewPic.setImageURI(Uri.fromFile(new File(imgPath)));
